package com.supergzt.entity.base;

import com.supergzt.enums.HttpEnum;
import lombok.Getter;
import lombok.Setter;

import java.io.Serial;
import java.io.Serializable;

public class Result<T> implements Serializable {
	@Serial
	private static final long serialVersionUID = 1L;
	@Getter
	@Setter
	private Integer code;
	@Getter
	@Setter
	private String msg;
	@Getter
	@Setter
	private T data;
	public static <T> Result<T> ok() {
		return restResult(HttpEnum.SUCCESS.getCode(), HttpEnum.SUCCESS.getMessage(), null);
	}
	public static <T> Result<T> ok(String msg) {
		return restResult(HttpEnum.SUCCESS.getCode(),msg, null);
	}

	public static <T> Result<T> ok(String msg,T data) {
		return restResult(HttpEnum.SUCCESS.getCode(),msg, data);
	}

	public static <T> Result<T> success(T data) {
		return restResult(HttpEnum.SUCCESS.getCode(), HttpEnum.SUCCESS.getMessage(), data);
	}

	private static <T> Result<T> restResult(Integer code,String msg, T data) {
		return new Result<T>().setCode(code).setMsg(msg).setData(data);
	}

	public static <T> Result<T> error(int code, String msg) {
		return restResult(code, msg,null);
	}

	public static <T> Result<T> error(String msg) {
		return restResult(HttpEnum.FAIL.getCode(), msg,null);
	}

	public static <T> Result<T> error() {
		return restResult(HttpEnum.FAIL.getCode(), HttpEnum.FAIL.getMessage(),null);
	}

	public static <T> Result<T> error(String msg, T data) {
		return restResult(HttpEnum.FAIL.getCode(),msg, data);
	}

	public static <T> boolean isOk(Result<T> result){
		return result.getCode().equals(HttpEnum.SUCCESS.getCode());
	}
}
